package com.zxt.dental_sys.Controller.Freeback;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageInfo;
import com.github.pagehelper.page.PageMethod;
import com.zxt.dental_sys.model.EnvironmentalFeedbackInfo;
import com.zxt.dental_sys.model.vo.RestBean;
import com.zxt.dental_sys.service.impl.EnvironmentalFeedbackInfoServiceImpl;
import com.zxt.dental_sys.service.impl.SysElementUserinfoServiceImpl;
import com.zxt.dental_sys.util.JwtUtil;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.List;
import java.util.UUID;

@RestController
@RequestMapping("/api/freeback")
public class FreebackController {
    /**
     * 未整改
     * 已整改
     */
    @Resource
    EnvironmentalFeedbackInfoServiceImpl feedbackInfoService;

    @Resource
    JwtUtil jwtUtil;
    @Autowired
    private SysElementUserinfoServiceImpl sysElementUserinfoServiceImpl;

    @RequestMapping("add")
    public RestBean insertFreeback(@RequestBody EnvironmentalFeedbackInfo feedbackInfo, HttpServletRequest request) {
        String uuid = jwtUtil.getJwtUUID(request);
        feedbackInfo.setPatientId(uuid);
        feedbackInfo.setInputTime(new Date());
        feedbackInfo.setStatus("未整改");
        feedbackInfo.setEnvironmentalFeedbackId(UUID.randomUUID().toString());
        return feedbackInfoService.save(feedbackInfo) ? RestBean.success(feedbackInfo) : RestBean.failure(500, "server error");
    }

    @RequestMapping("/list")
    public RestBean list() {
        List<EnvironmentalFeedbackInfo> list = feedbackInfoService.list(new QueryWrapper<EnvironmentalFeedbackInfo>().eq("is_deleted", 0).orderByDesc("input_time").orderByDesc("update_time"));
        return RestBean.success(list);
    }

    @RequestMapping("/lists/{page}/{size}")
    public RestBean lists(@PathVariable int page, @PathVariable int size,String name) {
        PageMethod.startPage(page, size);
        List<EnvironmentalFeedbackInfo> list = feedbackInfoService.list(
                new QueryWrapper<EnvironmentalFeedbackInfo>().eq("is_deleted", 0)
                        .orderByDesc("input_time")
                        .orderByDesc("update_time")
                        .like("contents",name)
        );
        list.forEach(el -> {
            el.setPatientId(JSON.toJSONString(sysElementUserinfoServiceImpl.getById(el.getPatientId())));
        });
        return RestBean.success(new PageInfo(list));
    }

    @RequestMapping("/listOfMy")
    public RestBean listMy(HttpServletRequest request) {
        String uuid = jwtUtil.getJwtUUID(request);
        List<EnvironmentalFeedbackInfo> list = feedbackInfoService.list(new QueryWrapper<EnvironmentalFeedbackInfo>().eq("patient_id", uuid).eq("is_deleted", 0));
        return RestBean.success(list);
    }

}
